/*
* ============================================================================
* 功能描述 ：lcd屏显
* 作者       ：ggls
* 创建日期 ：2024.04.24
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/


#include "main.h"


unsigned char table[10] = {250,10,188,
						158,78,214,
					246,138,254,222};

unsigned char seg[9];



void Write_Lcd_Storage(unsigned char cmd,unsigned char val)
{	
	unsigned char	ch_l = 0,ch_h = 0;
	switch(cmd)
	{
		   case temp_1:
				seg[2] = (seg[2] & 0x01)|table[val];
			   break;
		   
		   case temp_10:
			   seg[1] = (seg[1] & 0x01)|table[val];
			   break;
		   case temp_100:
			   seg[0] = (seg[0] & 0x01)|table[val];
			   break;
		   case hum_1:
		   	
			   break;
		   case hum_10:
		   		ch_h = (table[val] & 0xf0) >> 4;
				ch_l = (table[val] & 0x0f);
				seg[4] = (seg[4] & 0xf0)|(ch_h << 0);
		   		seg[5] = (seg[5] & 0x1f)|(ch_l << 4);
			   break;
		   case hum_100:
			   ch_h = (table[val] & 0xf0) >> 4;
			   ch_l = (table[val] & 0x0f);
			   seg[3] = (seg[3] & 0xf0)|(ch_h << 0);
			   seg[4] = (seg[4] & 0x1f)|(ch_l << 4);

			   break;
		   case face_state:
				if(val == 1)
				{	
					seg[3] = (seg[3] & 0xef)|(1 << 4);
					seg[2] = (seg[2] & 0xfe);
				}
				if(val == 0)
				{
					seg[2] = (seg[2] & 0xfe)|1;
					seg[3] = (seg[3] & 0xef);
				}
			   break;
		   case temp_unit:
				if(val == 1)
				{	
					seg[3] = (seg[3] & 0x1f)|(0x06 << 5);
				}
				if(val == 0)
				{
					seg[3] = (seg[3] & 0x1f)|(0x05 << 5);
				}
			   break;
		   case vol_state:
		   		if(val == 1)	
			   		seg[8] = (seg[8] & 0xF7);
				else
			   		seg[8] = (seg[8] & 0xF7) | (0x01 << 3);
					
			   break;
		   case net_state:
		   		if(val == 1)
				{	
					seg[4] = (seg[4] & 0xef);
				}
				if(val == 0)
				{
					seg[4] = (seg[4] & 0xef) | (0x01 << 4);
				}

			   break;
		   case min_1:
			   ch_h = (table[val] & 0xf0) >> 4;
			   ch_l = (table[val] & 0x0f);
			   seg[5] = (seg[5] & 0xf1)|(ch_l << 0);
			   seg[6] = (seg[6] & 0x0f)|(ch_h << 4);

			   break;
		   case min_2:
			   ch_h = (table[val] & 0xf0) >> 4;
			   ch_l = (table[val] & 0x0f);
			   seg[6] = (seg[6] & 0xf1)|(ch_l << 0);
			   seg[7] = (seg[7] & 0x0f)|(ch_h << 4);

			   break;
		   case hour_1:
			   ch_h = (table[val] & 0xf0) >> 4;
			   ch_l = (table[val] & 0x0f);
			   seg[7] = (seg[7] & 0xf1)|(ch_l << 0);
			   seg[8] = (seg[8] & 0x0f)|(ch_h << 4);

			   break;
		   case hour_2:
		   	if(val == 0)
		   	{
				seg[8] = (seg[8] & 0xF8)|0x00;
			}
			if(val == 1)
		   	{
				seg[8] = (seg[8] & 0xF8)|0x03;
			}
			if(val == 2)
		   	{
				seg[8] = (seg[8] & 0xF8)|0x06;
			}
			   break;
		   default:
			   break;

		}


}

void Seg_Init(void)
{
	seg[0] = seg[0] | 1;
	seg[1] = seg[1]	| 1;
	seg[3] = (seg[3] & 0x1f) | (0x05 << 5);
	seg[4] = (seg[4] & 0xef) | (0x01 << 4);
	seg[5] = (seg[5] & 0xef) | (0x01 << 4);
	seg[6] = seg[6]	| 0x01;
	seg[7] = seg[7] & 0xfe;
	seg[8] = (seg[8] & 0xF7);
}

void Seg_san_off(void)
{
	LCD_I2C_Start();
		
	LCD_write_byte(0x7C);
	LCD_read_ACK();
	LCD_write_byte(0xB6);
	LCD_read_ACK();
	LCD_write_byte(0xF0);
	LCD_read_ACK();
	LCD_write_byte(0xFe);	// 开启LCD
	LCD_read_ACK();
	LCD_write_byte(0xC8);
	LCD_read_ACK();
		
	LCD_I2C_Stop();

}

void Seg_san_on(void)
{
	LCD_I2C_Start();
		
	LCD_write_byte(0x7C);
	LCD_read_ACK();
	LCD_write_byte(0xB6);
	LCD_read_ACK();
	LCD_write_byte(0xF0);
	LCD_read_ACK();
	LCD_write_byte(0xFd);	// 关闭LCD
	LCD_read_ACK();
	LCD_write_byte(0xC8);
	LCD_read_ACK();
	
	LCD_I2C_Stop();

}

void Seg_Set_Data(void)
{

	int i;

	LCD_I2C_Start();

	LCD_write_byte(0x7C);
	LCD_read_ACK();
	LCD_write_byte(0xea);
	LCD_read_ACK();
	LCD_write_byte(0xf0);
	LCD_read_ACK();
	LCD_write_byte(0xb8);
	LCD_read_ACK();
	LCD_write_byte(0xe8);
	LCD_read_ACK();
	LCD_write_byte(0x08);
	LCD_read_ACK();
	
    LCD_write_byte(seg[0]);
	LCD_read_ACK();
   
	 LCD_write_byte(seg[1]);
	LCD_read_ACK();
    LCD_write_byte(seg[2]);
	LCD_read_ACK();
    LCD_write_byte(seg[3]);
	LCD_read_ACK();
    LCD_write_byte(seg[4]);
	LCD_read_ACK();
    LCD_write_byte(seg[5]);
	LCD_read_ACK();
    LCD_write_byte(seg[6]);
	LCD_read_ACK();
    LCD_write_byte(seg[7]);
	LCD_read_ACK();
    LCD_write_byte(seg[8]);
	LCD_read_ACK();
   
	
	
	LCD_I2C_Stop();
	LCD_I2C_Start();

	
	LCD_write_byte(0x7c);
	LCD_read_ACK();
	LCD_write_byte(0xca);
	LCD_read_ACK();
	LCD_I2C_Stop();

}

